package com.wgl.express.UI;

import com.wgl.express.entity.User;
import com.wgl.express.service.Impl.UserServiceImpl;
import com.wgl.express.service.UserService;
import com.wgl.express.util.Util;

import static com.wgl.express.text.Text.*;

public class Register {
    private String username;
    private String password;
    private String comfirmPassword;
    private Util util;
    private String phone;

    public void start(){
        util=  Util.getUtil();


        eventLoop();
    }

    private void eventLoop(){
        boolean exit = false;
        util.say(TITLE);
        util.say(MENU_REGISTR);
        while (!exit){

            util.readLine();
            util.say("请输入用户名：");
            this.username = util.readLine();
            util.say("请输入密码：");
            this.password = util.readLine();
            util.say("请再次输入密码：");
            this.comfirmPassword = util.readLine();
            util.say("请输入电话号码：");
            this.phone = util.readLine();

            test();

            if (register()){
                util.say("注册成功！请登录！\n\n");
                exit = true;
            }else{
                util.say("请重试!\n");
                util.say("返回请输入0，继续请输入1:");
                if (util.readInt() == 0){
                    return;
                }
            }
        }


    }

    private void test() {
        util.say(username+password+comfirmPassword+phone);
    }

    private boolean register() {
        if (!password.equals(comfirmPassword)){
            util.say("密码不一样！");
            return false;
        }
        UserService userService = new UserServiceImpl();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setType(1);
        int i=0;
        if (( i = userService.register(user)) == 0){
            util.say("用户名已存在！");
        }
        return i==1;
    }

}
